﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Xfs
{
    [XfsObjectSystem]
    public class XfsUserComponentSystem : XfsAwakeSystem<XfsUserComponent>
    {
        public override void Awake(XfsUserComponent self)
        {
            if (XfsGame.XfsSence.Type != XfsSenceType.XfsServer) return;

            for (int i = 0; i < 10; i++)
            {
                XfsUser? user = XfsComponentFactory.CreateWithParent<XfsUser>(self);

                if (user == null) continue;

                user.Id = 202201310000 + i;
                user.Username = "Tumo" + i.ToString();
                user.Password = "1234";

                if (!self.Users.TryGetValue(user.Username, out XfsUser us))
                {
                    self.Users.Add(user.Username, user);
                }
            }
        }
    }

    public static class XfsUserComponentHelper
    {
        public static XfsUser? CheckPassword(this XfsUserComponent self, string username,string password)
        {
            if (self.Users.TryGetValue(username, out XfsUser? us))
            {
                if (password == us.Password)
                {
                    Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + self.GetType() + " 40. 用户名: " + username + " 验证通过！");
                    
                    return us;
                }
            }
            Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + self.GetType() + " 43. 用户名: " + username + " 不在存！");
            
            return null;
        }

        public static bool UserInnerLogin(this XfsUserComponent self, string username, string password)
        {
            if (self.Users.TryGetValue(username, out XfsUser? us))
            {
                if (password == us.Password)
                {
                    Console.WriteLine(XfsTimeHelper.CurrentTime() + " " + self.GetType() + " 53. 用户名: " + username + " 登录通过！");
                    
                    return true;
                }
            }

            return false;
        }
        
        //public static void UserLogin(this XfsUserComponent self, string username, string password)
        //{
        //    if (user != null)
        //    {
        //        S4C_User response = new S4C_User() { RpcId = message.RpcId, Id = user.Id, Username = user.Username, Password = user.Password };

        //        reply(response);

        //        ///发送小怪到服务器
        //        XfsGame.XfsSence.GetComponent<XfsMonsterComponent>().SetMonsters(session);
        //    }
        //}





    }
}
